{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import math\n",
    "from scipy.optimize import fsolve"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# fsolve()求解函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([0.41640296]), {'nfev': 9, 'fjac': array([[-1.]]), 'r': array([-1.82660859]), 'qtf': array([2.32924791e-13]), 'fvec': array([0.])}, 1, 'The solution converged.')\n",
      "(array([0.56714329, 0.56714329]), {'nfev': 16, 'fjac': array([[-0.71438715,  0.69975067],\n",
      "       [-0.69975067, -0.71438715]]), 'r': array([-107.07100209,  107.04806463,   -2.2161566 ]), 'qtf': array([4.51781575e-15, 4.06157915e-13]), 'fvec': array([-1.11022302e-16,  2.22044605e-16])}, 1, 'The solution converged.')\n",
      "(array([0.56714329, 0.56714329]), {'nfev': 16, 'fjac': array([[-0.71438715,  0.69975067],\n",
      "       [-0.69975067, -0.71438715]]), 'r': array([-107.07100209,  107.04806463,   -2.2161566 ]), 'qtf': array([4.51781575e-15, 4.06157915e-13]), 'fvec': array([-1.11022302e-16,  2.22044605e-16])}, 1, 'The solution converged.')\n"
     ]
    }
   ],
   "source": [
    "def f(x):\n",
    "    return 2 * x - x**2 -np.exp(-x)\n",
    "x0 = 0\n",
    "x = fsolve(f, 0, full_output=True)\n",
    "print(x)\n",
    "\n",
    "def f(x):\n",
    "    return [\n",
    "        2 * x[0] - x[1] -np.exp(-x[0]), \n",
    "        -x[0] + 2 * x[1] - np.exp(-x[1])\n",
    "    ]\n",
    "x = fsolve(f, [-5, -5], full_output=True)\n",
    "print(x)\n",
    "\n",
    "def f(x, a, b):\n",
    "    return [\n",
    "        a * x[0] - x[1] -np.exp(-x[0]), \n",
    "        -x[0] + b * x[1] - np.exp(-x[1])\n",
    "    ]\n",
    "a = b = 2\n",
    "x = fsolve(f, [-5, -5], args=(a,b), full_output=True)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基础版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Va= 263495329.73880228\n",
      "AssetTheta= 0.1551119725025175\n",
      "DD= 3.388573310729276\n",
      "EDF= 0.00035128622532961816\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "from scipy.optimize import fsolve\n",
    "import numpy as np\n",
    "\n",
    "def KMVOptSearch(E, D, r, T, EquityTheta): # 计算KMV，返回值\n",
    "    EtoD = float(E) / float(D)\n",
    "\n",
    "    def KMVfun(x, EtoD, r, T, EquityTheta): # 构建方程式d1，d2，通过fsolve()求解得出VA和σA\n",
    "        EtoD, r, T, EquityTheta = float(EtoD), float(r), float(T), float(EquityTheta)\n",
    "        d1 = (np.log(x[0] * EtoD) + (r + 0.5 * x[1] ** 2) * T) / (x[1] * np.sqrt(T))\n",
    "        d2 = d1 - x[1] * np.sqrt(T)\n",
    "        return [\n",
    "            x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,\n",
    "            stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta\n",
    "        ]\n",
    "\n",
    "    VaThetaX = fsolve(KMVfun, [1, 0.1], args=(EtoD, r, T, EquityTheta))\n",
    "    Va = VaThetaX[0] * E\n",
    "    AssetTheta = VaThetaX[1]\n",
    "    return Va, AssetTheta\n",
    "\n",
    "r = 0.0225  # 无风险利率\n",
    "T = 1  # 期权到期时间，以年为单位\n",
    "SD = 1e8  # LD为长期负债 （单位：元）\n",
    "LD = 50000000  # TD短期负债 （单位：元）\n",
    "DP = SD + 0.5 * LD  # 违约触发点DPT # DP=（TD-LD） +0.5LD （7） 其中：TD为总负债；LD为长期负债。 （单位：元）\n",
    "D = DP  # D=TD-LD+0.5∗LD  债券的面值是D （单位：元）\n",
    "PriceTheta = 0.2893  # 资产收益率的波动率为σ （单位：%）\n",
    "EquityTheta = PriceTheta  # 股权收益率的波动率σE\n",
    "E = 141276427  # 公司股票的市场价值 （单位：元）\n",
    "Va, AssetTheta = KMVOptSearch(E, D, r, T, EquityTheta)  # 资产价值VA（单位：元）资产收益率波动率σA\n",
    "AssetTheta = AssetTheta  # 资产收益率波动率σA （单位：%）\n",
    "DD = (Va - DP) / (Va * AssetTheta)  # 违约距离DD \n",
    "EDF = stats.norm.cdf(-DD)  # 预期违约概率EDF（单位：%）\n",
    "\n",
    "# In[20]:\n",
    "\n",
    "\n",
    "print('Va=', Va)\n",
    "print('AssetTheta=', AssetTheta)\n",
    "print('DD=', DD)\n",
    "print('EDF=', EDF)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# KMV数据汇总表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1季度的情况：\n",
      "Va= 4001492647.0\n",
      "AssetTheta= 0.1\n",
      "DD= -60.6650516656566\n",
      "EDF= 1.0\n",
      "\n",
      "\n",
      "第2季度的情况：\n",
      "Va= 2998874817.0\n",
      "AssetTheta= 0.1\n",
      "DD= -80.52233740505615\n",
      "EDF= 1.0\n",
      "\n",
      "\n",
      "第3季度的情况：\n",
      "Va= 2059107657.0\n",
      "AssetTheta= 0.1\n",
      "DD= -89.63890524253438\n",
      "EDF= 1.0\n",
      "\n",
      "\n",
      "第4季度的情况：\n",
      "Va= 2594834800.0\n",
      "AssetTheta= 0.1\n",
      "DD= -65.76701613528537\n",
      "EDF= 1.0\n",
      "\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\Anaconda\\lib\\site-packages\\scipy\\optimize\\minpack.py:163: RuntimeWarning: The iteration is not making good progress, as measured by the \n",
      "  improvement from the last ten iterations.\n",
      "  warnings.warn(msg, RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "from scipy.optimize import fsolve\n",
    "import numpy as np\n",
    "\n",
    "def KMVOptSearch(E, D, r, T, EquityTheta):\n",
    "    EtoD = float(E) / float(D)\n",
    "    def KMVfun(x, EtoD, r, T, EquityTheta):\n",
    "        EtoD, r, T, EquityTheta = float(EtoD), float(r), float(T), float(EquityTheta)\n",
    "        d1 = (np.log(x[0] * EtoD) + (r + 0.5 * x[1] ** 2) * 1) / (x[1] * np.sqrt(T))\n",
    "        d2 = d1 - x[1] * np.sqrt(T)\n",
    "        return [\n",
    "            x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,\n",
    "            stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta\n",
    "        ]\n",
    "    VaThetaX = fsolve(KMVfun, [1, 0.1], args=(EtoD, r, T, EquityTheta))\n",
    "    Va=VaThetaX[0] * E\n",
    "    AssetTheta=VaThetaX[1]\n",
    "    return Va, AssetTheta\n",
    "\n",
    "DP_list = [28276568464, 27146515802, 20516723272, 19660289016]\n",
    "D_list = [30983551225, 29893388116, 23062745183, 22235856439]\n",
    "PriceTheta_list = [0.020635364, 0.034431294, 0.030177996, 0.031205325]\n",
    "E_list  = [4001492647, 2998874817, 2059107657, 2594834800]\n",
    "\n",
    "for i in range(4):\n",
    "    r = 0.0225  # 无风险利率\n",
    "    T = 1   # 期权到期时间，以年为单位\n",
    "    # SD = 1e8  #  LD为长期负债\n",
    "    # LD = 50000000 # TD为总负债x\n",
    "    DP = DP_list[i] # 违约触发点DPT # DP=（TD-LD） +0.5LD （7） 其中：TD为总负债；LD为长期负债。 \n",
    "    D = D_list[i] # D=TD-LD+0.5∗LD  债券的面值是D\n",
    "    PriceTheta =  PriceTheta_list[i]  # 交易日的对数收益率 当是月交易率则\n",
    "    EquityTheta = PriceTheta   # 股权收益率的波动率 σE\n",
    "    E =  E_list[i]   # 公司股票的市场价值\n",
    "    Va, AssetTheta = KMVOptSearch(E,D,r,T,EquityTheta) # 资产价值VA（单位：元）,资产收益率波动率σA\n",
    "    AssetTheta = AssetTheta # 资产收益率波动率σA\n",
    "    DD=(Va-DP)/(Va*AssetTheta) # 违约距离DD\n",
    "    EDF=stats.norm.cdf(-DD) # 预期违约概率EDF（单位：%）\n",
    "    \n",
    "    print(\"第{}季度的情况：\".format(i + 1))\n",
    "    print('Va=', Va)\n",
    "    print('AssetTheta=', AssetTheta )\n",
    "    print('DD=', DD)\n",
    "    print('EDF=', EDF)\n",
    "    print(\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 广州浪奇违约距离计算表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EtoD： 3.590551586186523\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1 11\n",
      "-1.9662544925580505e-08 10.584499894052435\n",
      "\n",
      "第2次计算：\n",
      "1.0 11.0\n",
      "-1.9662544925580505e-08 10.584499894052435\n",
      "\n",
      "第3次计算：\n",
      "1.0 11.0\n",
      "-1.9662544925580505e-08 10.584499894052435\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 11.0\n",
      "-4.761383842755151e-09 10.584500057965208\n",
      "\n",
      "第5次计算：\n",
      "1.0 11.000000163912773\n",
      "-1.9662535821751703e-08 10.584500057965256\n",
      "\n",
      "第6次计算：\n",
      "1.0000006075331949 0.4154965201339511\n",
      "-0.27155492364637546 -0.0001743324860354334\n",
      "\n",
      "第7次计算：\n",
      "1.3783157158522776 -3.7457255836791195\n",
      "-1.236693706884862 -0.4699235163741434\n",
      "\n",
      "第8次计算：\n",
      "0.8843114555636199 1.5521672476357853\n",
      "-0.3048047359170424 0.8717308001174497\n",
      "\n",
      "第9次计算：\n",
      "1.0000006224343652 0.4154965201339511\n",
      "-0.2715549087513416 -0.0001743262750772967\n",
      "\n",
      "第10次计算：\n",
      "1.0000006075331949 0.41549652632533174\n",
      "-0.27155492363718325 -0.0001743263241268389\n",
      "\n",
      "第11次计算：\n",
      "1.2718362385596729 0.3018259269234789\n",
      "0.0002310702612255966 -0.03162687516244034\n",
      "\n",
      "第12次计算：\n",
      "1.2715580964009936 0.33367534621842004\n",
      "-4.700500307386335e-05 0.00878723955443994\n",
      "\n",
      "第13次计算：\n",
      "1.2716051427391593 0.32675042746016697\n",
      "1.2303844165728606e-08 -2.68796125391102e-06\n",
      "\n",
      "第14次计算：\n",
      "1.2716051304223053 0.32675254508665263\n",
      "-6.02895511292445e-12 7.656986711346292e-10\n",
      "\n",
      "第15次计算：\n",
      "1.2716051304283311 0.3267525444835751\n",
      "-5.218048215738236e-15 8.005818230572004e-13\n",
      "\n",
      "第1季度的情况：\n",
      "Va= 9772263434.930994\n",
      "AssetTheta= 0.3267525444835751\n",
      "DD= 2.390122462919958\n",
      "EDF=0.8421377835081907%\n",
      "\n",
      "EtoD： 2.791926856940518\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1 11\n",
      "-2.24384136826572e-08 10.367899878596466\n",
      "\n",
      "第2次计算：\n",
      "1.0 11.0\n",
      "-2.24384136826572e-08 10.367899878596466\n",
      "\n",
      "第3次计算：\n",
      "1.0 11.0\n",
      "-2.24384136826572e-08 10.367899878596466\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 11.0\n",
      "-7.537252599831845e-09 10.367900042509238\n",
      "\n",
      "第5次计算：\n",
      "1.0 11.000000163912773\n",
      "-2.2438403246560767e-08 10.367900042509293\n",
      "\n",
      "第6次计算：\n",
      "1.0000006825483314 0.6320959841233691\n",
      "-0.3446046402054703 -0.015520631600863699\n",
      "\n",
      "第7次计算：\n",
      "1.5419323225988901 -5.304742046140148\n",
      "-1.3463280996927125 -0.6459265160947634\n",
      "\n",
      "第8次计算：\n",
      "0.7971029645155967 2.645914723774131\n",
      "-0.2984948563562154 1.3686506008700512\n",
      "\n",
      "第9次计算：\n",
      "1.0000006974495028 0.6320959841233691\n",
      "-0.344604625670101 -0.015520621555426328\n",
      "\n",
      "第10次计算：\n",
      "1.0000006825483314 0.6320959935423333\n",
      "-0.34460463966332866 -0.015520623137227463\n",
      "\n",
      "第11次计算：\n",
      "1.3685749298259455 0.3728529325536187\n",
      "0.016550908822273502 -0.12185577403957804\n",
      "\n",
      "第12次计算：\n",
      "1.3442267661389258 0.5175019568010527\n",
      "-0.007281044822101679 0.06200890940053316\n",
      "\n",
      "第13次计算：\n",
      "1.351868447154066 0.4677864843725658\n",
      "2.121056977744118e-05 -0.0003052731243092799\n",
      "\n",
      "第14次计算：\n",
      "1.3518419563164412 0.4680482395896015\n",
      "-4.133017135643513e-06 3.281753759709627e-05\n",
      "\n",
      "第15次计算：\n",
      "1.3518456997130244 0.4680254429332105\n",
      "-4.911002008389787e-07 4.049963772900789e-06\n",
      "\n",
      "第16次计算：\n",
      "1.3518462101655642 0.4680222087463433\n",
      "4.978104151120988e-09 -4.105694750844435e-08\n",
      "\n",
      "第17次计算：\n",
      "1.3518462050430888 0.46802224120477737\n",
      "-1.1546319456101628e-13 9.446887716535457e-13\n",
      "\n",
      "第2季度的情况：\n",
      "Va= 9083106960.021572\n",
      "AssetTheta= 0.46802224120477737\n",
      "DD= 1.5705388233587485\n",
      "EDF=5.814490484994523%\n",
      "\n",
      "EtoD： 3.0363621167962287\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1 11\n",
      "-2.152619082984586e-08 10.632399883682657\n",
      "\n",
      "第2次计算：\n",
      "1.0 11.0\n",
      "-2.152619082984586e-08 10.632399883682657\n",
      "\n",
      "第3次计算：\n",
      "1.0 11.0\n",
      "-2.152619082984586e-08 10.632399883682657\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 11.0\n",
      "-6.625029858042808e-09 10.632400047595429\n",
      "\n",
      "第5次计算：\n",
      "1.0 11.000000163912773\n",
      "-2.1526180726816335e-08 10.632400047595484\n",
      "\n",
      "第6次计算：\n",
      "1.0000006768716536 0.36759612747325754\n",
      "-0.32437436547254905 -0.00021872411573947703\n",
      "\n",
      "第7次计算：\n",
      "1.488199596352166 -5.002260992769447\n",
      "-1.3161517533627975 -0.3862916917840814\n",
      "\n",
      "第8次计算：\n",
      "0.8306350540645782 2.1014122294101063\n",
      "-0.31368250957840527 1.2608572313592588\n",
      "\n",
      "第9次计算：\n",
      "1.0000006917728248 0.36759612747325754\n",
      "-0.32437435058009745 -0.00021871861070332788\n",
      "\n",
      "第10次计算：\n",
      "1.0000006768716536 0.3675961329508667\n",
      "-0.32437436546129017 -0.00021871867374001486\n",
      "\n",
      "第11次计算：\n",
      "1.3248129205221817 0.24703359061170577\n",
      "0.00037469437836867847 -0.04032670832440538\n",
      "\n",
      "第12次计算：\n",
      "1.32435499631638 0.2877218445621128\n",
      "-8.321196031568778e-05 0.013445770187493988\n",
      "\n",
      "第13次计算：\n",
      "1.324438316839424 0.2775430150342915\n",
      "9.720272098334704e-08 -1.1431592835009496e-05\n",
      "\n",
      "第14次计算：\n",
      "1.3244382197152103 0.2775516576823116\n",
      "8.443623578102688e-11 -1.192595211474412e-08\n",
      "\n",
      "第15次计算：\n",
      "1.3244382196307114 0.2775516667107027\n",
      "-5.6288307348495437e-14 8.109679594525687e-12\n",
      "\n",
      "第3季度的情况：\n",
      "Va= 6757422299.682708\n",
      "AssetTheta= 0.2775516667107027\n",
      "DD= 2.7070093630290075\n",
      "EDF=0.33946171279210113%\n",
      "\n",
      "EtoD： 2.2539830734124777\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1 11\n",
      "-2.504109675172117e-08 10.78259986405032\n",
      "\n",
      "第2次计算：\n",
      "1.0 11.0\n",
      "-2.504109675172117e-08 10.78259986405032\n",
      "\n",
      "第3次计算：\n",
      "1.0 11.0\n",
      "-2.504109675172117e-08 10.78259986405032\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 11.0\n",
      "-1.0139935668895816e-08 10.782600027963094\n",
      "\n",
      "第5次计算：\n",
      "1.0 11.000000163912773\n",
      "-2.5041085094379412e-08 10.782600027963156\n",
      "\n",
      "第6次计算：\n",
      "1.0000007918910747 0.21739551499085685\n",
      "-0.43705068127841795 -1.409722028364313e-05\n",
      "\n",
      "第7次计算：\n",
      "1.7887045278417335 -8.458316849858255\n",
      "-1.4370333527773962 -0.21748350276184258\n",
      "\n",
      "第8次计算：\n",
      "0.6459376793996314 4.0156686450053005\n",
      "-0.37770812934071996 2.3307140959854906\n",
      "\n",
      "第9次计算：\n",
      "1.0000008067922477 0.21739551499085685\n",
      "-0.43705066637791545 -1.4093978200929724e-05\n",
      "\n",
      "第10次计算：\n",
      "1.0000007918910747 0.21739551823030245\n",
      "-0.43705068127781344 -1.4093983216834083e-05\n",
      "\n",
      "第11次计算：\n",
      "1.43708889725188 0.12224146710701665\n",
      "3.50652052949485e-05 -0.04172814483672549\n",
      "\n",
      "第12次计算：\n",
      "1.4370460472242996 0.1639983979958728\n",
      "-7.784822281919901e-06 0.01827324959106133\n",
      "\n",
      "第13次计算：\n",
      "1.4370538323547346 0.15128169333394434\n",
      "3.081497279566747e-10 -6.282934178480915e-08\n",
      "\n",
      "第14次计算：\n",
      "1.4370538320466013 0.1512817370711268\n",
      "1.6431300764452317e-14 -2.327105175226052e-11\n",
      "\n",
      "第4季度的情况：\n",
      "Va= 7655605730.79823\n",
      "AssetTheta= 0.1512817370711268\n",
      "DD= 4.569433198261652\n",
      "EDF=0.00024452250484362383%\n",
      "\n",
      "EtoD： 1.9381926579583184\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1 11\n",
      "-2.702803036935819e-08 10.82189985291349\n",
      "\n",
      "第2次计算：\n",
      "1.0 11.0\n",
      "-2.702803036935819e-08 10.82189985291349\n",
      "\n",
      "第3次计算：\n",
      "1.0 11.0\n",
      "-2.702803036935819e-08 10.82189985291349\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 11.0\n",
      "-1.212686939755514e-08 10.82190001682626\n",
      "\n",
      "第5次计算：\n",
      "1.0 11.000000163912773\n",
      "-2.7028017823838013e-08 10.821900016826328\n",
      "\n",
      "第6次计算：\n",
      "1.0000008553126478 0.17809507795561963\n",
      "-0.5082601581422648 -1.3732264839222408e-05\n",
      "\n",
      "第7次计算：\n",
      "2.051481594032326 -11.388159875743462\n",
      "-1.5082631549463859 -0.1781000701524636\n",
      "\n",
      "第8次计算：\n",
      "0.4547964021767472 6.083703516278573\n",
      "-0.5463339005586789 2.585290658369117\n",
      "\n",
      "第9次计算：\n",
      "1.0000008702138217 0.17809507795561963\n",
      "-0.5082601432418409 -1.3729608057566622e-05\n",
      "\n",
      "第10次计算：\n",
      "1.0000008553126478 0.1780950806094431\n",
      "-0.5082601581417145 -1.3729613188878664e-05\n",
      "\n",
      "第11次计算：\n",
      "1.489885653093666 0.17971065852581955\n",
      "-0.01837751440211699 0.08964833167884398\n",
      "\n",
      "第12次计算：\n",
      "1.5082825495268042 0.08667964603697594\n",
      "1.9382004142087794e-05 -0.047362602483268984\n",
      "\n",
      "第13次计算：\n",
      "1.508255740800018 0.1301873551971234\n",
      "-7.426722644199302e-06 0.0182558258556324\n",
      "\n",
      "第14次计算：\n",
      "1.5082631675505893 0.11808295221218103\n",
      "2.7926994050631038e-11 1.6753726900242683e-07\n",
      "\n",
      "第15次计算：\n",
      "1.5082631675227467 0.11808284100055302\n",
      "8.43769498715119e-14 -2.0242105214229866e-10\n",
      "\n",
      "第5季度的情况：\n",
      "Va= 7221690918.180037\n",
      "AssetTheta= 0.11808284100055302\n",
      "DD= 5.571693357862731\n",
      "EDF=1.2613758277639869e-06%\n",
      "\n",
      "EtoD： 1.2900698228256007\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1 11\n",
      "-3.317860164386843e-08 10.855199818284593\n",
      "\n",
      "第2次计算：\n",
      "1.0 11.0\n",
      "-3.317860164386843e-08 10.855199818284593\n",
      "\n",
      "第3次计算：\n",
      "1.0 11.0\n",
      "-3.317860164386843e-08 10.855199818284593\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 11.0\n",
      "-1.827744056104308e-08 10.855199982197366\n",
      "\n",
      "第5次计算：\n",
      "1.0 11.000000163912773\n",
      "-3.317858610074609e-08 10.85519998219745\n",
      "\n",
      "第6次计算：\n",
      "1.0000010625297178 0.14479402295078359\n",
      "-0.7620729691216778 -0.0038419438607574796\n",
      "\n",
      "第7次计算：\n",
      "2.3947756638363957 -15.152391797685508\n",
      "-1.7636113349627387 -0.14480000000036006\n",
      "\n",
      "第8次计算：\n",
      "0.30071521289375647 7.772209853096471\n",
      "-0.6993332867085411 2.192228045261938\n",
      "\n",
      "第9次计算：\n",
      "1.0000010774308947 0.14479402295078359\n",
      "-0.7620729546152868 -0.003841940846076225\n",
      "\n",
      "第10次计算：\n",
      "1.0000010625297178 0.14479402510838266\n",
      "-0.7620729689893009 -0.0038419419973039493\n",
      "\n",
      "第11次计算：\n",
      "1.4913775644008336 0.10122237715486553\n",
      "-0.27223377056165876 0.006160782302070983\n",
      "\n",
      "第12次计算：\n",
      "1.7653017397708446 0.06334440352200993\n",
      "0.0016904048080579326 -0.032978014257849464\n",
      "\n",
      "第13次计算：\n",
      "1.7613107516494158 0.10141035237378512\n",
      "-0.0023005833133711295 0.03381514396450355\n",
      "\n",
      "第14次计算：\n",
      "1.7635207569778497 0.0828821606591243\n",
      "-9.057798493694591e-05 0.00136441070553861\n",
      "\n",
      "第15次计算：\n",
      "1.7636047190616961 0.08216030720759007\n",
      "-6.615901090523124e-06 9.830551086453809e-05\n",
      "\n",
      "第16次计算：\n",
      "1.763611304903947 0.08210450607078934\n",
      "-3.005883963957956e-08 4.350899988070367e-07\n",
      "\n",
      "第17次计算：\n",
      "1.7636113347212716 0.08210425998312187\n",
      "-2.4151503019709253e-10 3.5351358507185182e-09\n",
      "\n",
      "第18次计算：\n",
      "1.7636113349636258 0.082104257960385\n",
      "8.391065620116933e-13 -1.2287532102916998e-11\n",
      "\n",
      "第6季度的情况：\n",
      "Va= 6705071539.414016\n",
      "AssetTheta= 0.082104257960385\n",
      "DD= 6.826377711589298\n",
      "EDF=4.354273068781948e-10%\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "from scipy.optimize import fsolve\n",
    "import numpy as np\n",
    "\n",
    "def KMVOptSearch(E, D, r, T, EquityTheta):\n",
    "    EtoD = float(E) / float(D)\n",
    "    print(\"EtoD：\", EtoD)\n",
    "    print()\n",
    "    global i \n",
    "    i = 1\n",
    "    def KMVfun(x, EtoD, r, T, EquityTheta):\n",
    "        EtoD, r, T, EquityTheta = float(EtoD), float(r), float(T), float(EquityTheta)\n",
    "        d1 = (np.log(x[0] * EtoD) + (r + 0.5 * x[1] ** 2) * T) / (x[1] * np.sqrt(T))\n",
    "        d2 = d1 - x[1] * np.sqrt(T)\n",
    "        global i\n",
    "        print(\"第{}次计算：\".format(i))\n",
    "        i += 1\n",
    "        print(x[0],x[1])\n",
    "        print(x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,\n",
    "           stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta\n",
    "        )\n",
    "        print()\n",
    "        return [\n",
    "            x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,\n",
    "            stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta\n",
    "        ]\n",
    "    print()\n",
    "    VaThetaX = fsolve(KMVfun, [1, 11], args=(EtoD, r, T, EquityTheta))\n",
    "    Va = VaThetaX[0] * E\n",
    "    AssetTheta = VaThetaX[1]\n",
    "    return Va, AssetTheta\n",
    "\n",
    "\"\"\"\n",
    "时间\t20150630\t20151231\t20160630\t20161231\t20170630\t20171231\n",
    "股权市场价值\t7,684,982,705\t6,719,038,694\t5,102,104,575\t5,327,292,242\t4,788,084,118\t3,801,898,642\n",
    "股权收益率波动率\t0.4155\t0.6321\t0.3676\t0.2174\t0.1781\t0.1448\n",
    "违约触发点\t2,140,334,854\t2,406,595,530\t1,680,334,683\t2,363,501,441\t2,470,386,057\t2,947,048,737\n",
    "无风险利率\t2.51%\t1.73%\t1.50%\t1.50%\t1.50%\t1.50%\n",
    "资产市场价值\t9,772,328,015\t9,083,051,041\t6,757,422,300\t7,655,605,731\t7,221,690,918\t6,705,071,539\n",
    "资产收益率波动率\t0.3267\t0.4681\t0.2775\t0.1513\t0.1181\t0.0821\n",
    "违约距离\t2.3903\t1.5704\t2.7071\t4.5688\t5.5724\t6.8286\n",
    "预期违约率\t0.84%\t5.82%\t0.34%\t0.00%\t0.00%\t0.00%\n",
    "\"\"\"\n",
    "# 无风险列表\n",
    "r_list = [0.0251, 0.0173, 0.0150, 0.0150, 0.0150, 0.0150]\n",
    "# 违约触发点列表\n",
    "DP_list = [2140334854, 2406595530, 1680334683, 2363501441, 2470386057, 2947048737]\n",
    "# 债券面值列表\n",
    "D_list = [30983551225, 29893388116, 23062745183, 22235856439]\n",
    "# 股权收益率波动率列表\n",
    "PriceTheta_list = [0.4155, 0.6321, 0.3676, 0.2174, 0.1781, 0.1448]\n",
    "# 股权市场价值列表\n",
    "E_list  = [7684982705, 6719038694, 5102104575, 5327292242, 4788084118, 3801898642]\n",
    "\n",
    "for time in range(6):\n",
    "    # 利用KMV模型计算\n",
    "    r = r_list[time]  # 无风险利率\n",
    "    T = 1  # 期权到期时间，以年为单位\n",
    "    SD = 1e8  # LD为长期负债 （单位：元）\n",
    "    LD = 50000000  # TD短期负债 （单位：元）\n",
    "    DP = DP_list[time]  # 违约触发点DPT # DP=（TD-LD） +0.5LD （7） 其中：TD为总负债；LD为长期负债。 （单位：元）\n",
    "    D = DP  # D=TD-LD+0.5∗LD  债券的面值是D （单位：元）\n",
    "    PriceTheta = PriceTheta_list[time]  # 交易波动率，可以是日月年的数据 （单位：%）\n",
    "    EquityTheta = PriceTheta  # 股权收益率的波动率σE\n",
    "    E = E_list[time]  # 公司股票的市场价值 （单位：元）\n",
    "    Va, AssetTheta = KMVOptSearch(E, D, r, T, EquityTheta)  # 资产价值VA（单位：元）资产收益率波动率σA\n",
    "    AssetTheta = AssetTheta  # 资产收益率波动率σA （单位：%）\n",
    "    DD = (Va - DP) / (Va * AssetTheta)  # 违约距离DD \n",
    "    EDF = stats.norm.cdf(-DD)  # 预期违约概率EDF（单位：%）\n",
    "    \n",
    "    print(\"第{}季度的情况：\".format(time + 1))    \n",
    "    print('Va=', Va)\n",
    "    print('AssetTheta=', AssetTheta)\n",
    "    print('DD=', DD)\n",
    "    print('EDF={}%'.format(EDF * 100))\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EtoD： 3.5956507725759894\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1.0 0.1\n",
      "-0.27397319759652594 0.03399727100000001\n",
      "\n",
      "第2次计算：\n",
      "1.0 0.1\n",
      "-0.27397319759652594 0.03399727100000001\n",
      "\n",
      "第3次计算：\n",
      "1.0 0.1\n",
      "-0.27397319759652594 0.03399727100000001\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.27397318269536475 0.03399727249011612\n",
      "\n",
      "第5次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.27397319759652594 0.03399727249011612\n",
      "\n",
      "第6次计算：\n",
      "1.2739731975971236 0.03860540911356378\n",
      "5.977440764581843e-13 -0.016820472507048018\n",
      "\n",
      "第7次计算：\n",
      "1.2739731975965334 0.05521040686771239\n",
      "7.549516567451064e-15 0.004333849577865159\n",
      "\n",
      "第8次计算：\n",
      "1.2739731975965258 0.05180856954017622\n",
      "-1.1102230246251565e-16 2.7755575615628914e-16\n",
      "\n",
      "第9次计算：\n",
      "1.273973197596526 0.051808569540176\n",
      "0.0 0.0\n",
      "\n",
      "第1季度的情况：\n",
      "Va= 10773594428.272057\n",
      "AssetTheta= 0.051808569540176\n",
      "DD= 15.088155299786903\n",
      "EDF=9.689236906395817e-50%\n",
      "\n",
      "EtoD： 2.7222303983784673\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1.0 0.1\n",
      "-0.3618767684725943 0.143044375\n",
      "\n",
      "第2次计算：\n",
      "1.0 0.1\n",
      "-0.3618767684725943 0.143044375\n",
      "\n",
      "第3次计算：\n",
      "1.0 0.1\n",
      "-0.3618767684725943 0.143044375\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.3618767535714331 0.14304437649011612\n",
      "\n",
      "第5次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.3618767684725943 0.14304437649011612\n",
      "\n",
      "第6次计算：\n",
      "1.3618767684733837 -0.07923205238053219\n",
      "-1.0 0.043044375\n",
      "\n",
      "第7次计算：\n",
      "0.8233502540420454 0.19787713709629562\n",
      "-0.5385261312582562 0.20596485113673527\n",
      "\n",
      "第8次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.3618767535714331 0.14304437649011612\n",
      "\n",
      "第9次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.3618767684725943 0.14304437649011612\n",
      "\n",
      "第10次计算：\n",
      "1.3618767684733837 -0.07923205238053219\n",
      "-1.0 0.043044375\n",
      "\n",
      "第11次计算：\n",
      "0.8233502540420454 0.19787713709629562\n",
      "-0.5385261312582562 0.20596485113673527\n",
      "\n",
      "第12次计算：\n",
      "1.0883248729789772 0.05106143145185217\n",
      "-0.27355189549361714 0.09861580089896177\n",
      "\n",
      "第13次计算：\n",
      "1.2679422765890553 -0.04119951497916868\n",
      "-1.0 0.043044375\n",
      "\n",
      "第14次计算：\n",
      "0.9890220677521284 0.03332384712831354\n",
      "-0.3728547007204659 0.07600239519230048\n",
      "\n",
      "第15次计算：\n",
      "1.0883248891962816 0.05106143145185217\n",
      "-0.27355187927631275 0.09861580172704054\n",
      "\n",
      "第16次计算：\n",
      "1.0883248729789772 0.05106143221272679\n",
      "-0.27355189549361714 0.09861580172704054\n",
      "\n",
      "第17次计算：\n",
      "1.13563475725665 0.034936779669326706\n",
      "-0.22624201121594423 0.0827197962991049\n",
      "\n",
      "第18次计算：\n",
      "1.23009032615886 0.002279604624236979\n",
      "-0.13178644231373426 0.04584849459574091\n",
      "\n",
      "第19次计算：\n",
      "1.361876768469901 -0.038250150011592716\n",
      "-1.0 0.043044375\n",
      "\n",
      "第20次计算：\n",
      "1.1927384102520122 -0.03262685990646057\n",
      "-1.0 0.043044375\n",
      "\n",
      "第21次计算：\n",
      "1.2300903444886342 0.002279604624236979\n",
      "-0.13178642398396012 0.04584849463752555\n",
      "\n",
      "第22次计算：\n",
      "1.23009032615886 0.002279604658205735\n",
      "-0.13178644231373426 0.04584849463752555\n",
      "\n",
      "第23次计算：\n",
      "1.2776899655446265 -0.011328164892014419\n",
      "-1.0 0.043044375\n",
      "\n",
      "第24次计算：\n",
      "1.214551491969614 -0.0139442176784648\n",
      "-1.0 0.043044375\n",
      "\n",
      "第25次计算：\n",
      "1.2426767350757106 0.0028735304991053447\n",
      "-0.11920003339688368 0.04661524449876871\n",
      "\n",
      "第26次计算：\n",
      "1.2303988702980984 0.00521309861081971\n",
      "-0.13147789817449596 0.04945856564150516\n",
      "\n",
      "第27次计算：\n",
      "1.2486928386562945 0.0044113717308384\n",
      "-0.11318392981629977 0.048552823288948735\n",
      "\n",
      "第28次计算：\n",
      "1.2486928572632678 0.0044113717308384\n",
      "-0.11318391120932647 0.04855282337103101\n",
      "\n",
      "第29次计算：\n",
      "1.2486928386562945 0.004411371796572961\n",
      "-0.11318392981629977 0.04855282337103101\n",
      "\n",
      "第30次计算：\n",
      "1.2515863126260953 0.00340551145408978\n",
      "-0.11029045584649899 0.047306666523430164\n",
      "\n",
      "第31次计算：\n",
      "1.2573733556212487 0.0013939680306245365\n",
      "-0.10450341285134557 0.04479711326029512\n",
      "\n",
      "第32次计算：\n",
      "1.2689485512066754 -0.002627050416859157\n",
      "-1.0 0.043044375\n",
      "\n",
      "第33次计算：\n",
      "1.2535663085687152 -0.002649694907450183\n",
      "-1.0 0.043044375\n",
      "\n",
      "第34次计算：\n",
      "1.257373374357572 0.0013939680306245365\n",
      "-0.10450339411502241 0.044797113286412955\n",
      "\n",
      "第35次计算：\n",
      "1.2573733556212487 0.001393968051396279\n",
      "-0.10450341285134557 0.044797113286412955\n",
      "\n",
      "第36次计算：\n",
      "1.2602677428949844 0.00039674368223032797\n",
      "-0.10160902557760987 0.04354437826491226\n",
      "\n",
      "第37次计算：\n",
      "1.2660567675595342 -0.001597241183518182\n",
      "-1.0 0.043044375\n",
      "\n",
      "第38次计算：\n",
      "1.2583910535519398 -0.0016272025973845662\n",
      "-1.0 0.043044375\n",
      "\n",
      "第39次计算：\n",
      "1.2602677616744373 0.00039674368223032797\n",
      "-0.10160900679815699 0.04354437827236289\n",
      "\n",
      "第40次计算：\n",
      "1.2602677428949844 0.00039674368814226953\n",
      "-0.10160902557760987 0.04354437827236289\n",
      "\n",
      "第41次计算：\n",
      "1.2617150959611878 -0.00010042827461170865\n",
      "-1.0 0.043044375\n",
      "\n",
      "第42次计算：\n",
      "1.2597999898933065 -0.00010891763621345815\n",
      "-1.0 0.043044375\n",
      "\n",
      "第43次计算：\n",
      "1.2606616247221978 0.0004037847255947289\n",
      "-0.10121514375039653 0.04355341090820626\n",
      "\n",
      "第44次计算：\n",
      "1.2604805610996834 0.0004656630500820592\n",
      "-0.10139620737291088 0.04363133422265083\n",
      "\n",
      "第45次计算：\n",
      "1.2607347185874624 0.00045643138556662776\n",
      "-0.10114204988513187 0.04361981389443683\n",
      "\n",
      "第46次计算：\n",
      "1.2607017437777408 0.0004856005422853678\n",
      "-0.10117502469485351 0.04365657245043858\n",
      "\n",
      "第47次计算：\n",
      "1.2607512059923234 0.0004418468072072576\n",
      "-0.10112556248027094 0.04360143389505041\n",
      "\n",
      "第48次计算：\n",
      "1.2607841835712637 0.00041267964136495534\n",
      "-0.10109258490133066 0.0435646749647148\n",
      "\n",
      "第49次计算：\n",
      "1.2608501498114566 0.0003543532789163095\n",
      "-0.10102661866113771 0.04349116138480781\n",
      "\n",
      "第50次计算：\n",
      "1.260982126664654 0.00023773247688232513\n",
      "-0.10089464180794039 0.04334415140427633\n",
      "\n",
      "第51次计算：\n",
      "1.2612462582116144 4.61893287296162e-06\n",
      "-0.10063051026097991 0.04305020061180296\n",
      "\n",
      "第52次计算：\n",
      "1.2617752354757954 -0.0004610929459299369\n",
      "-1.0 0.043044375\n",
      "\n",
      "第53次计算：\n",
      "1.260902423189929 -0.00014876818156480868\n",
      "-1.0 0.043044375\n",
      "\n",
      "第54次计算：\n",
      "1.2612462770056483 4.61893287296162e-06\n",
      "-0.10063049146694603 0.043050200611889765\n",
      "\n",
      "第55次计算：\n",
      "1.2612462582116144 4.618932941789083e-06\n",
      "-0.10063051026097991 0.043050200611889765\n",
      "\n",
      "第56次计算：\n",
      "1.2614272312057107 -5.7381641500841256e-05\n",
      "-1.0 0.043044375\n",
      "\n",
      "第57次计算：\n",
      "1.2611880852244362 -5.871514437196971e-05\n",
      "-1.0 0.043044375\n",
      "\n",
      "第58次计算：\n",
      "1.2612954914201238 5.567495086659562e-06\n",
      "-0.10058127705247055 0.04305139725645131\n",
      "\n",
      "第59次计算：\n",
      "1.2612728013695547 1.3189495949944898e-05\n",
      "-0.10060396710303965 0.04306101055250544\n",
      "\n",
      "第60次计算：\n",
      "1.2613047340398158 1.2048797626087722e-05\n",
      "-0.10057203443277851 0.04305957220548527\n",
      "\n",
      "第61次计算：\n",
      "1.2613006677429888 1.5731657044068234e-05\n",
      "-0.10057610072960554 0.04306421734953439\n",
      "\n",
      "第62次计算：\n",
      "1.2613067671882292 1.0207367917098523e-05\n",
      "-0.10057000128436511 0.04305724962222902\n",
      "\n",
      "第63次计算：\n",
      "1.2613108335301004 6.524540078843284e-06\n",
      "-0.10056593494249388 0.043052604473085246\n",
      "\n",
      "第2季度的情况：\n",
      "Va= 1489565797.601557\n",
      "AssetTheta= 6.524540078843284e-06\n",
      "DD= 108629.67638539756\n",
      "EDF=0.0%\n",
      "\n",
      "EtoD： 0.9559133081900637\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1.0 0.1\n",
      "-0.9729687658347264 -0.47929577666065554\n",
      "\n",
      "第2次计算：\n",
      "1.0 0.1\n",
      "-0.9729687658347264 -0.47929577666065554\n",
      "\n",
      "第3次计算：\n",
      "1.0 0.1\n",
      "-0.9729687658347264 -0.47929577666065554\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.9729687598600569 -0.479295770302655\n",
      "\n",
      "第5次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.9729687652586729 -0.47929577586106276\n",
      "\n",
      "第6次计算：\n",
      "7.708721002854533 -4.341259202518065\n",
      "-1.9527925856142123 -0.6605289023940373\n",
      "\n",
      "第7次计算：\n",
      "-1.8504008796074878 2.816997464084801\n",
      "nan nan\n",
      "\n",
      "第8次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.9729687598600569 -0.479295770302655\n",
      "\n",
      "第9次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.9729687652586729 -0.47929577586106276\n",
      "\n",
      "第10次计算：\n",
      "7.708721002854533 -4.341259202518065\n",
      "-1.9527925856142123 -0.6605289023940373\n",
      "\n",
      "第11次计算：\n",
      "-1.8504008796074878 2.816997464084801\n",
      "nan nan\n",
      "\n",
      "第12次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第13次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第14次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第15次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第16次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第17次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第3季度的情况：\n",
      "Va= 2330557525.0\n",
      "AssetTheta= 0.1\n",
      "DD= -0.46119968654281557\n",
      "EDF=67.76723267183924%\n",
      "\n",
      "EtoD： 7.78062359839415\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1.0 0.1\n",
      "-0.12661092355198633 -0.15948147199999999\n",
      "\n",
      "第2次计算：\n",
      "1.0 0.1\n",
      "-0.12661092355198633 -0.15948147199999999\n",
      "\n",
      "第3次计算：\n",
      "1.0 0.1\n",
      "-0.12661092355198633 -0.15948147199999999\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.12661090865082514 -0.15948147050988387\n",
      "\n",
      "第5次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.12661092355198633 -0.15948147050988387\n",
      "\n",
      "第6次计算：\n",
      "1.1266109235522626 0.2468203802392364\n",
      "2.7622348852673895e-13 0.018589064532846755\n",
      "\n",
      "第7次计算：\n",
      "1.126610923552005 0.22948475313628722\n",
      "1.865174681370263e-14 -0.0009414423280235673\n",
      "\n",
      "第8次计算：\n",
      "1.1266109235519863 0.23032039418000919\n",
      "0.0 -2.3314683517128287e-15\n",
      "\n",
      "第9次计算：\n",
      "1.1266109235519863 0.23032039418001127\n",
      "0.0 5.551115123125783e-17\n",
      "\n",
      "第4季度的情况：\n",
      "Va= 2872226340.0640335\n",
      "AssetTheta= 0.23032039418001127\n",
      "DD= 3.8464654652926544\n",
      "EDF=0.005991700611522841%\n",
      "\n",
      "EtoD： 0.7350433597278652\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1.0 0.1\n",
      "-0.9999431889939554 0.006531258048916848\n",
      "\n",
      "第2次计算：\n",
      "1.0 0.1\n",
      "-0.9999431889939554 0.006531258048916848\n",
      "\n",
      "第3次计算：\n",
      "1.0 0.1\n",
      "-0.9999431889939554 0.006531258048916848\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.9999431889641621 0.006531258146346294\n",
      "\n",
      "第5次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.9999431889845104 0.0065312580800258495\n",
      "\n",
      "第6次计算：\n",
      "93.08992112900773 -15.68665393542963\n",
      "-2.3402092904665457 0.006331318999641399\n",
      "\n",
      "第7次计算：\n",
      "-38.980508653458806 10.755435710532485\n",
      "nan nan\n",
      "\n",
      "第8次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.9999431889641621 0.006531258146346294\n",
      "\n",
      "第9次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.9999431889845104 0.0065312580800258495\n",
      "\n",
      "第10次计算：\n",
      "93.08992112900773 -15.68665393542963\n",
      "-2.3402092904665457 0.006331318999641399\n",
      "\n",
      "第11次计算：\n",
      "-38.980508653458806 10.755435710532485\n",
      "nan nan\n",
      "\n",
      "第12次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第13次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第14次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第15次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第16次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第17次计算：\n",
      "nan nan\n",
      "nan nan\n",
      "\n",
      "第5季度的情况：\n",
      "Va= 3972150000.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AssetTheta= 0.1\n",
      "DD= -3.604639600720013\n",
      "EDF=99.98437068027454%\n",
      "\n",
      "EtoD： 2.7452683146928187\n",
      "\n",
      "\n",
      "第1次计算：\n",
      "1.0 0.1\n",
      "-0.35883994811388464 0.152323031\n",
      "\n",
      "第2次计算：\n",
      "1.0 0.1\n",
      "-0.35883994811388464 0.152323031\n",
      "\n",
      "第3次计算：\n",
      "1.0 0.1\n",
      "-0.35883994811388464 0.152323031\n",
      "\n",
      "第4次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.35883993321272345 0.1523230324901161\n",
      "\n",
      "第5次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.35883994811388464 0.1523230324901161\n",
      "\n",
      "第6次计算：\n",
      "1.3588399481146674 -0.08820702637924649\n",
      "-1.0 0.052323031\n",
      "\n",
      "第7次计算：\n",
      "0.8220394465555879 0.1968620430927066\n",
      "-0.5368002085733967 0.21415006933100067\n",
      "\n",
      "第8次计算：\n",
      "1.0000000149011612 0.1\n",
      "-0.35883993321272345 0.1523230324901161\n",
      "\n",
      "第9次计算：\n",
      "1.0 0.10000000149011612\n",
      "-0.35883994811388464 0.1523230324901161\n",
      "\n",
      "第10次计算：\n",
      "1.3588399481146674 -0.08820702637924649\n",
      "-1.0 0.052323031\n",
      "\n",
      "第11次计算：\n",
      "0.8220394465555879 0.1968620430927066\n",
      "-0.5368002085733967 0.21415006933100067\n",
      "\n",
      "第12次计算：\n",
      "1.0909119874376756 0.05533917787385147\n",
      "-0.26792796067620905 0.11269320351753034\n",
      "\n",
      "第13次计算：\n",
      "1.2730412730879077 -0.03335200596419713\n",
      "-1.0 0.052323031\n",
      "\n",
      "第14次计算：\n",
      "0.9921569257347287 0.03315493701925886\n",
      "-0.36668302237915595 0.08521793138595642\n",
      "\n",
      "第15次计算：\n",
      "1.090912003693531 0.05533917787385147\n",
      "-0.2679279444203537 0.11269320441711601\n",
      "\n",
      "第16次计算：\n",
      "1.0909119874376756 0.05533917869846948\n",
      "-0.26792796067620905 0.11269320441711603\n",
      "\n",
      "第17次计算：\n",
      "1.1373459972727116 0.03682622193469096\n",
      "-0.22149395084117307 0.0942071871120973\n",
      "\n",
      "第18次计算：\n",
      "1.2300464271628968 -0.0005544073396924407\n",
      "-1.0 0.052323031\n",
      "\n",
      "第19次计算：\n",
      "1.0932684802777208 0.013940780103435656\n",
      "-0.2655714678361638 0.06756404647756899\n",
      "\n",
      "第20次计算：\n",
      "1.1373460142204876 0.03682622193469096\n",
      "-0.221493933893397 0.09420718773621986\n",
      "\n",
      "第21次计算：\n",
      "1.1373459972727116 0.03682622248344443\n",
      "-0.22149395084117307 0.09420718773621986\n",
      "\n",
      "第22次计算：\n",
      "1.1605505688882718 0.02792231211248231\n",
      "-0.19828937922561285 0.08472828620681722\n",
      "\n",
      "第23次计算：\n",
      "1.2069251349019285 0.010044298127967567\n",
      "-0.1519148132119561 0.06444574687309244\n",
      "\n",
      "第24次计算：\n",
      "1.2997810442511413 -0.02549468682768117\n",
      "-1.0 0.052323031\n",
      "\n",
      "第25次计算：\n",
      "1.1698888320712477 -0.020415401281760354\n",
      "-1.0 0.052323031\n",
      "\n",
      "第26次计算：\n",
      "1.2069251528865146 0.010044298127967567\n",
      "-0.15191479522737006 0.06444574705373499\n",
      "\n",
      "第27次计算：\n",
      "1.2069251349019285 0.010044298277639273\n",
      "-0.1519148132119561 0.06444574705373499\n",
      "\n",
      "第28次计算：\n",
      "1.2301811125272029 0.001752966758899785\n",
      "-0.12865883558668179 0.05447949759768654\n",
      "\n",
      "第29次计算：\n",
      "1.2767063505096468 -0.014803840108543548\n",
      "-1.0 0.052323031\n",
      "\n",
      "第30次计算：\n",
      "1.2133823313729157 -0.013997544553207904\n",
      "-1.0 0.052323031\n",
      "\n",
      "第31次计算：\n",
      "1.23018113085833 0.001752966758899785\n",
      "-0.12865881725555472 0.0544794976298204\n",
      "\n",
      "第32次计算：\n",
      "1.2301811125272029 0.001752966785021025\n",
      "-0.12865883558668179 0.0544794976298204\n",
      "\n",
      "第33次计算：\n",
      "1.241818737330867 -0.0022958918609552216\n",
      "-1.0 0.052323031\n",
      "\n",
      "第34次计算：\n",
      "1.2260692484305469 -0.002172773267572441\n",
      "-1.0 0.052323031\n",
      "\n",
      "第35次计算：\n",
      "1.2333432807158176 0.0017717102384229013\n",
      "-0.12549666739806709 0.054508157917934306\n",
      "\n",
      "第36次计算：\n",
      "1.2320104734592547 0.002466631958255731\n",
      "-0.1268294746546299 0.05536194740674037\n",
      "\n",
      "第37次计算：\n",
      "1.2337743813837214 0.0023130800306796766\n",
      "-0.12506556673016322 0.055176849883942856\n",
      "\n",
      "第38次计算：\n",
      "1.2339970460390457 0.002579893897518298\n",
      "-0.12484290207483895 0.05550661244863174\n",
      "\n",
      "第39次计算：\n",
      "1.2344488960771753 0.0031098506128210274\n",
      "-0.1243910520367093 0.056161982655961847\n",
      "\n",
      "第40次计算：\n",
      "1.2353782399380366 0.004154860071536102\n",
      "-0.123461708175848 0.057455854722363094\n",
      "\n",
      "第41次计算：\n",
      "1.2373361026016247 0.006183545803235466\n",
      "-0.1215038455122599 0.059974155464434006\n",
      "\n",
      "第42次计算：\n",
      "1.2416226723862835 0.009982518829082822\n",
      "-0.1172172757276011 0.06471755270571221\n",
      "\n",
      "第43次计算：\n",
      "1.25148435732433 0.016453639414057752\n",
      "-0.1073555907895547 0.07291450334774832\n",
      "\n",
      "第44次计算：\n",
      "1.2748642432039676 0.02434761943506348\n",
      "-0.08397570490991701 0.08336294042490042\n",
      "\n",
      "第45次计算：\n",
      "1.3250643422462762 0.0191867927587258\n",
      "-0.033775605867608416 0.07774676592665661\n",
      "\n",
      "第46次计算：\n",
      "1.392357596472279 -0.04255293682952309\n",
      "-1.0 0.052323031\n",
      "\n",
      "第47次计算：\n",
      "1.28100323998934 -0.0011315563459176406\n",
      "-1.0 0.052323031\n",
      "\n",
      "第48次计算：\n",
      "1.3250643619912736 0.0191867927587258\n",
      "-0.033775586122611 0.07774676630549979\n",
      "\n",
      "第49次计算：\n",
      "1.3250643422462762 0.01918679304463129\n",
      "-0.033775605867608416 0.07774676630549979\n",
      "\n",
      "第50次计算：\n",
      "1.3347249103584653 0.0014537413143935335\n",
      "-0.024115037755419344 0.05426337574553831\n",
      "\n",
      "第51次计算：\n",
      "1.3544380301934151 -0.033888059177022055\n",
      "-1.0 0.052323031\n",
      "\n",
      "第52次计算：\n",
      "1.310581581204424 -0.004276858350163685\n",
      "-1.0 0.052323031\n",
      "\n",
      "第53次计算：\n",
      "1.3347249302474162 0.0014537413143935335\n",
      "-0.024115017866468413 0.05426337577445169\n",
      "\n",
      "第54次计算：\n",
      "1.3347249103584653 0.001453741336055967\n",
      "-0.024115037755419344 0.05426337577445169\n",
      "\n",
      "第55次计算：\n",
      "1.339826475851819 -0.007261411191168118\n",
      "-1.0 0.052323031\n",
      "\n",
      "第56次计算：\n",
      "1.3287926809981703 -0.0001972728890115372\n",
      "-1.0 0.052323031\n",
      "\n",
      "第57次计算：\n",
      "1.3371240121441346 -9.744539407837132e-05\n",
      "-1.0 0.052323031\n",
      "\n",
      "第58次计算：\n",
      "1.3332187023769138 0.0010916208454847976\n",
      "-0.025621245736970888 0.05377840032710483\n",
      "\n",
      "第59次计算：\n",
      "1.3339534488213203 0.001323681071282898\n",
      "-0.024886499292564368 0.05408875993017732\n",
      "\n",
      "第60次计算：\n",
      "1.3343300121191188 0.0014406392999205464\n",
      "-0.024509935994765808 0.05424531925452226\n",
      "\n",
      "第61次计算：\n",
      "1.3345371045638417 0.0015001005687158525\n",
      "-0.024302843550042974 0.05432497086952862\n",
      "\n",
      "第62次计算：\n",
      "1.3348104022584542 0.0014910608327420932\n",
      "-0.02402954585543049 0.0543133145099443\n",
      "\n",
      "第6季度的情况：\n",
      "Va= 2260733508.6177497\n",
      "AssetTheta= 0.0014537413143935335\n",
      "DD= 500.1491581089116\n",
      "EDF=0.0%\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\Anaconda\\lib\\site-packages\\scipy\\optimize\\minpack.py:163: RuntimeWarning: The iteration is not making good progress, as measured by the \n",
      "  improvement from the last ten iterations.\n",
      "  warnings.warn(msg, RuntimeWarning)\n",
      "F:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:13: RuntimeWarning: invalid value encountered in log\n",
      "  del sys.path[0]\n",
      "F:\\Anaconda\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in greater\n",
      "  return (self.a < x) & (x < self.b)\n",
      "F:\\Anaconda\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in less\n",
      "  return (self.a < x) & (x < self.b)\n",
      "F:\\Anaconda\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:1738: RuntimeWarning: invalid value encountered in greater_equal\n",
      "  cond2 = (x >= self.b) & cond0\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "from scipy.optimize import fsolve\n",
    "import numpy as np\n",
    "\n",
    "def KMVOptSearch(E, D, r, T, EquityTheta):\n",
    "    EtoD = float(E) / float(D)\n",
    "    print(\"EtoD：\",EtoD)\n",
    "    print()\n",
    "    global i \n",
    "    i = 1\n",
    "    def KMVfun(x, EtoD, r, T, EquityTheta):\n",
    "        EtoD, r, T, EquityTheta = float(EtoD), float(r), float(T), float(EquityTheta)\n",
    "        d1 = (np.log(x[0] * EtoD) + (r + 0.5 * x[1] ** 2) * T) / (x[1] * np.sqrt(T))\n",
    "        d2 = d1 - x[1] * np.sqrt(T)\n",
    "        global i\n",
    "        print(\"第{}次计算：\".format(i))\n",
    "        i += 1\n",
    "        print(x[0],x[1])\n",
    "        print(x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,\n",
    "           stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta\n",
    "        )\n",
    "        print()\n",
    "        return [\n",
    "            x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,\n",
    "            stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta\n",
    "        ]\n",
    "    print()\n",
    "    VaThetaX = fsolve(KMVfun, [1, 0.1], args=(EtoD, r, T, EquityTheta))\n",
    "    Va = VaThetaX[0] * E\n",
    "    AssetTheta = VaThetaX[1]\n",
    "    return Va, AssetTheta\n",
    "\n",
    "\"\"\"\n",
    "股权市场价值\t股权波动率\t交易天数\t违约触发点DPT\t年收益率\t无风险利率\t资产市场价值\t资产价值波动率\t1年后资产市场价值\t违约距离\t违约概率\n",
    "8456688452\t0.292019222\t202\t2351921526\t0.066002729\t0.015\t10773594428\t0.229219282\t11484681066\t3.698198789\t0.000108567\n",
    "1180966466\t0.355101273\t149\t433823113.1\t-0.043044375\t0.015\t1608330787\t0.260744082\t1539101194\t2.635613491\t0.004199267\n",
    "2330557525\t0.512402342\t233\t2438042765\t0.519391101\t0.015\t2330557525\t0.1\t3541028365\t4.732711323\t1.1077E-06\n",
    "2549439456\t0.477213921\t243\t327665183.1\t0.259481472\t0.015\t2872226331\t0.423583627\t3617515846\t2.704072682\t0.003424765\n",
    "3972150000\t0.40510488\t133\t5403966919\t-0.006331319\t0.015\t3972150000\t0.1\t3947001050\t-3.667952791\t0.99987775\n",
    "1693782360\t0.39121959\t170\t616982446.1\t-0.052323031\t0.015\t2301579008\t0.287907325\t2181153419\t2.360508781\t0.009124943\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "# 无风险列表\n",
    "r_list = [0.015, 0.015, 0.015, 0.015, 0.015, 0.015]\n",
    "# 违约触发点列表\n",
    "DP_list = [2351921526, 433823113.1, 2438042765, 327665183.1, 5403966919, 616982446.1]\n",
    "# 债券面值列表\n",
    "D_list = [30983551225, 29893388116, 23062745183, 22235856439]\n",
    "# 股权收益率波动率列表\n",
    "PriceTheta_list = [0.066002729, -0.043044375, 0.519391101, 0.259481472, -0.006331319, -0.052323031]\n",
    "# 股权市场价值列表\n",
    "E_list  = [8456688452, 1180966466, 2330557525, 2549439456, 3972150000, 1693782360]\n",
    "\n",
    "for time in range(6):\n",
    "    # 利用KMV模型计算\n",
    "    r = r_list[time]  # 无风险利率\n",
    "    T = 1  # 期权到期时间，以年为单位\n",
    "    SD = 1e8  # LD为长期负债 （单位：元）\n",
    "    LD = 50000000  # TD短期负债 （单位：元）\n",
    "    DP = DP_list[time]  # 违约触发点DPT # DP=（TD-LD） +0.5LD （7） 其中：TD为总负债；LD为长期负债。 （单位：元）\n",
    "    D = DP  # D=TD-LD+0.5∗LD  债券的面值是D （单位：元）\n",
    "    PriceTheta = PriceTheta_list[time]  # 交易波动率，可以是日月年的数据 （单位：%）\n",
    "    EquityTheta = PriceTheta  # 股权收益率的波动率σE\n",
    "    E = E_list[time]  # 公司股票的市场价值 （单位：元）\n",
    "    Va, AssetTheta = KMVOptSearch(E, D, r, T, EquityTheta)  # 资产价值VA（单位：元）资产收益率波动率σA\n",
    "    AssetTheta = AssetTheta  # 资产收益率波动率σA （单位：%）\n",
    "    DD = (Va - DP) / (Va * AssetTheta)  # 违约距离DD \n",
    "    EDF = stats.norm.cdf(-DD)  # 预期违约概率EDF（单位：%）\n",
    "    \n",
    "    print(\"第{}季度的情况：\".format(time + 1))    \n",
    "    print('Va=', Va)\n",
    "    print('AssetTheta=', AssetTheta)\n",
    "    print('DD=', DD)\n",
    "    print('EDF={}%'.format(EDF * 100))\n",
    "    print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
